<template>
  <el-card shadow="hover">
    <!--  条件查询-->
    <el-form :inline="true" :model="studentSearch" class="demo-form-inline">

      <el-form-item>
        <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item >
        <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
      </el-form-item>
      <el-form-item >
        <el-select v-model="studentSearch.cid" placeholder="所在班级">
          <el-option label="所有班级" value="0"/>
          <el-option :label="c.cname" :value="c.cid" v-for="c in classes"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="success" @click="addUI">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="students"
      style="width: 100%">
      <el-table-column
        prop="sid"
        align="center"
        label="id"/>
      <el-table-column
        prop="sname"
        align="center"
        label="学生姓名"/>

      <el-table-column
        prop="sex"
        align="center"
        label="姓别"/>
      <el-table-column
        prop="age"
        align="center"
        label="年龄"/>
      <el-table-column
        prop="addr"
        align="center"
        label="住址"/>
<!--      <el-table-column-->
<!--        prop="cid"-->
<!--        align="center"-->
<!--        label="所在班级"/>-->
      <el-table-column
        prop="cname"
        align="center"
        label="所在班级"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      align="right">
    </el-pagination>
<!--    添加框-->
    <el-dialog :title="student.sid ? '修改':'添加'" :visible.sync="dialogFormVisible">
      <el-form :model="student">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="student.sname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="student.sex">
            <el-radio label="男" value="男"/>
            <el-radio label="女" value="女"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input v-model="student.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="student.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="班级" :label-width="formLabelWidth">
            <el-select v-model="student.cid" pldialogFormVisibleaceholder="请选择所在班级">
              <el-option label="班级" value="0"/>
              <el-option :label="c.cname" :value="c.cid" v-for="c in classes"/>
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>

</template>


<script>
  import {findStudents,findByPage,search,save} from '@/api/student'
  import {findClasses} from "@/api/classes";

  export default {
    data() {
      return {
        students: [],
        classes:[],
        studentSearch:{},
        student:{},
        dialogFormVisible:false,
        page:1,
        pageSize:5,
        total:100,
        formLabelWidth:'100px'

      }
    },
    created() {
      // 查询所有学生
      // this.findStudents();
      //开始分页查询
      // this.findByPage();
      //查询所有班级
      this.findClasses();
      //条件查询带分页
      this.search();

    },
    methods: {
      //1.1 查询所有学生
      findStudents(){
        findStudents().then(({data})=>{
          this.students = data.lists;
        })
      },
      // 分页查询
      findByPage(){
        findByPage(this.page,this.pageSize).then(({data})=>{
          // 更新总记录数
          this.total = data.pr.totalPage;
          // 更新当前页面的总的记录集合
          this.students = data.pr.rows;
        })
      },
      // 查询所有班级
      findClasses(){
        findClasses().then(({data})=>{
          this.classes = data.list;
        })
      },
      // 开始条件查询带分页
      search(){
        search(this.page,this.pageSize,this.studentSearch).then(({data})=>{
          // 更新总记录数
          this.total = data.pr.totalPage;
          // 更新当前页面的总的记录集合
          this.students = data.pr.rows;
        })
      },
      //添加学生
      addUI(){
        this.dialogFormVisible=true
        this.student ={}
      },
      // 添加
      save(){
        //根据是否有sid来确定是添加还是修改
        let url ='student/add'
        if (this.student.sid){
          url ='student/update'
        }
        save(url,this.student).then((data)=>{
          const _this =this
          if (data.code == 20000){
            console.log("添加成功1")
            this.$message({
              showClose: true,
              message: data.message,
              type: 'success',
              onClose(){
                //关闭对话框
                _this.dialogFormVisible=false;
                //重新刷新
                _this.search()
              }
            });
          }
        })
      },
      //修改
      handleEdit(index, row) {
        //打开对话框
        this.dialogFormVisible=true
        //我i当前表单中的元素赋值
        this.student=row
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      // 页大小发生改变时调用
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        // this.findByPage();
        this.search();
        //this.search();
      },
      // 当前页大小发生改变时调用
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val;
         // this.findByPage();
        this.search();
      }
    }
  }
</script>

<style scoped>
  .el-pagination{
    margin-top: 50px;
  }
</style>
